Skip to main content

English

Install Python packages

Requirements to install packages

Requirements to install packages networked (must have Internet access)

  • Connect to login0 of MareNostrum4:

    mylaptop$> ssh {username}@mn0.bsc.es
    IMPORTANT

    The MareNostrum4 login0 is restricted to BSC staff and is only accessible from the BSC internal network or the Virtual Private Network (VPN).

  • Check the Internet connectivity from login0, for example:

    $> wget --tries=3 --timeout=5 -q --spider google.com && echo "Networked" || echo "Non-networked"
    Networked

Check the Python interpreter

  • Load Python in the session, for example:

    $> module purge && module load intel mkl python/3.10.2
  • Ensure you can run Python from the command line:

    $> which python3
    /apps/PYTHON/3.10.2/INTEL/bin/python3
    $> python3 --version
    Python 3.10.2

Check the 'pip' package manager

pip is the recommended package installer, allowing packages installation from PyPI (Python Package Index), local projects, or directly from distribution files. It also supports version control.

"python3 -m pip" executes pip using the Python interpreter you specified as python3.

  • Ensure you can run pip from the command line:

    $> python3 -m pip --version
    pip 21.2.4 from /apps/PYTHON/3.10.2/INTEL/lib/python3.10/site-packages/pip (python 3.10)

Install and manage packages

Install packages

  • Install a package from PyPI:

    $> python -m pip install SomePackage            # Latest version
    $> python -m pip install "SomePackage==1.0.4" # Specific version
    $> python -m pip install "SomePackage>=1.0.4" # Minimum version
  • Install a package locally (from PyPI):

    $> python3 -m pip install --user SomePackage    # At the user default location

    Or:

    $> export PYTHONUSERBASE=/path/to/my/python/local/project
    $> python3 -m pip install --user SomePackage
  • Install a list of packages specified in a requirements file:

    $> python3 -m pip install -r requirements.txt

    Maybe you want to do something like this:

    SomePythonEnv$> python3 -m pip freeze > requirements.txt

    MyPythonEnv$> python3 -m pip install -r requirements.txt
  • Install packages from a source file:

    $> python3 -m pip install SomePackage-1.0.4.tar.gz

Update packages

  • Upgrade an already installed package to the latest from PyPI:

    $> python3 -m pip install --upgrade SomePackage

    # Dependencies are upgraded only when they don't meet
    # the requirements of the upgraded packages

    Or:

    $> python3 -m pip install SomePackage --upgrade --upgrade-strategy eager

    # Dependencies are upgraded regardless of whether the currently
    # the currently installed version meets the requirements
    # of the upgraded package

Uninstall packages

  • Uninstall a package:

    $> python3 -m pip uninstall SomePackage
    $> python3 -m pip uninstall SomePackage --yes # Don't ask for confirmation, also '-y'
  • Uninstall a list of packages from a requirements file:

    $> python3 -m pip uninstall -r requirements.txt [--yes, -y]

Verification of installed packages

  • Show the list of existing packages:

    $> python3 -m pip list          # Packages that are globally installed
    $> python3 -m pip list --user # Packages that are locally installed

    Or:

    $> python3 -m pip freeze
    $> python3 -m pip freeze --user

    It can also be helpful:

    $> python3 -m pip list [--user] | grep -i SomePackage
    $> python3 -m pip freeze [--user] | grep -i SomePackage
  • Show the list of outdated packages (and show the latest version available):

    $> python3 -m pip list --outdated
  • Show location of existing packages:

    $> python3 -m site              # Globally installed packages
    $> python3 -m site --user-site # Locally installed packages
  • Show details of a specific package:

    $> python3 -m pip show SomePackage
  • Verify that installed packages have compatible dependencies:

    $> python3 -m pip check
    No broken requirements found. # The desirable

Work with environments